﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BUS;
using QLVBDQ.UIControl;
using DTO;
using System.Globalization;

namespace QLVBDQ
{
    /// <summary>
    /// Interaction logic for LapPhieuGiaCong.xaml
    /// </summary>
    public partial class LapPhieuGiaCong : UserControl
    {
        public LapPhieuGiaCong()
        {
            InitializeComponent();
        }

        private void HoaDonMoi_Click(object sender, RoutedEventArgs e)
        {
            LayNgayHienTai();
            this.hoTenTho.Text = string.Empty;
            this.diaChiTho.Text = string.Empty;
            this.tenGC.Text = string.Empty;
            this.donGiaGC.Text = string.Empty;
            this.soLuong.Text = string.Empty;
            this.maGC.Text = string.Empty;
            this.maTho.Text = string.Empty;
            this.listGiaCong.Items.Clear();
            TinhTongTien();

        }

        private void ThemDV_Click(object sender, RoutedEventArgs e)
        {
            ThemHangGiaCong();
        }

        private void XoaDV_Click(object sender, RoutedEventArgs e)
        {
            XoaHangGiaCong();
        }

        private void XuatHoaDon_Click(object sender, RoutedEventArgs e)
        {
            ThemPhieuGiaCong();
            
        }

        private void Thoat_Click(object sender, RoutedEventArgs e)
        {

        }

        private void listSanPham_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            
            maTho.MakeFocus();
        }

        private void LoadMaTho()
        {
            List<string> list = Tho_BUS.Instance.LayToanBoMaTho();
            if (list == null)
            {
                return;
            }
            for (int i = 0; i < list.Count; i++)
            {
                this.maTho.AddItem(new AutoCompleteEntry(list[i], list[i]));
            }
        }

        private void LayNgayHienTai()
        {
            this.ngayLap.DisplayDate = DateTime.Now;
            this.ngayLap.SelectedDate = DateTime.Now;

            this.ngayThanhToan.SelectedDate = DateTime.Now;
            this.ngayThanhToan.DisplayDate = DateTime.Now;
           
        }

        private void maTho_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.Key == Key.Tab)
            {
                Tho_DTO item = Tho_BUS.Instance.LayThongTinTho(maTho.Text.ToString());
                if (item ==null)
                {
                    return;
                }
                this.hoTenTho.Text = item.TenTho;
                this.diaChiTho.Text = item.DiaChi;
            }
        }

        private void maTho_LostFocus(object sender, RoutedEventArgs e)
        {
            Tho_DTO item = Tho_BUS.Instance.LayThongTinTho(maTho.Text.ToString());
            if (item == null)
            {
                this.hoTenTho.Text = string.Empty;
                this.diaChiTho.Text = string.Empty;
                return;
            }
            this.hoTenTho.Text = item.TenTho;
            this.diaChiTho.Text = item.DiaChi;
        }

        public void LoadMaHangGiaCong()
        {
            List<string> list = HangGiaCong_BUS.Instance.LayToanBoMaHangGiaCong();
            if (list == null)
            {
                return;
            }
            
            for (int i = 0; i < list.Count; i++)
            {
                maGC.AddItem(new AutoCompleteEntry(list[i], list[i]));
            }
        }

        private void maGC_LostFocus(object sender, RoutedEventArgs e)
        {
            HangGiaCong_DTO item = HangGiaCong_BUS.Instance.LayThongTinHangGiaCong(maGC.Text.ToString());
            if (item == null)
            {
                this.tenGC.Text = string.Empty;
                this.donGiaGC.Text = string.Empty;
                this.soLuong.Text = string.Empty;
                return;
            }
            this.tenGC.Text = item.TenHangGC;
            this.donGiaGC.Text = item.DonGia.ToString();
            this.soLuong.Focusable = true;
        }

        private void LoadMaPhieuGiaCong()
        {
            string item = PhieuGiaCong_BUS.Instance.LayMaPhieuGiaCong();
            if (item == null)
            {
                return;
            }
            this.maPhieu.Text = item;
        }

        public void TinhTongTien()
        {
            long tong = 0;
            for (int i = 0; i < listGiaCong.Items.Count;i++ )
            {
                HangGiaCong_DTO item = (HangGiaCong_DTO)listGiaCong.Items[i];
                tong += item.ThanhTien;
            }
            this.tongTien.Text = tong.ToString();
        }

        public void ThemHangGiaCong()
        {
            if (this.tenGC.Text=="" || this.soLuong.Text=="" || this.donGiaGC.Text=="" ||this.maGC.Text=="")
            {
                MessageBox.Show("Bạn Cần Điền Đầy Đủ Thông Tin!");
                return;
            }
            int sl = 0;
            long dongia = 0;
            try
            {
                sl = int.Parse(this.soLuong.Text.ToString());
                dongia= long.Parse(this.donGiaGC.Text.ToString());

            }
            catch (System.Exception ex)
            {
            	MessageBox.Show("Bạn Phải Nhập Số Lượng Hoặc Đơn Giá là 1 con số!");
                return;
            }

            long thanhtien = sl * dongia;
            HangGiaCong_DTO item = new HangGiaCong_DTO(this.maGC.Text, this.tenGC.Text, dongia, sl, thanhtien);
            item.STT = listGiaCong.Items.Count+1;
            listGiaCong.Items.Add(item);
            TinhTongTien();
            MessageBox.Show("Thêm hàng gia công thành công!");
            maGC.Text = string.Empty;
            tenGC.Text = string.Empty;
            soLuong.Text = string.Empty;
            donGiaGC.Text = string.Empty;
            maGC.MakeFocus();
        }

        public void XoaHangGiaCong()
        {
            if (listGiaCong.Items.Count==0)
            {
                this.tongTien.Text = "0";
                MessageBox.Show("Không thể xóa thêm được nữa!");
                return;
            }

            listGiaCong.Items.Remove((HangGiaCong_DTO)listGiaCong.SelectedItem);
            listGiaCong.Items.Refresh();
            TinhTongTien();
            CapNhapSTT();
        }

        private void CapNhapSTT()
        {

            for (int i = 0; i < this.listGiaCong.Items.Count; i++)
            {
                HangGiaCong_DTO item = (HangGiaCong_DTO)this.listGiaCong.Items[i];
                item.STT = i + 1;
                this.listGiaCong.Items[i] = item;
                this.listGiaCong.Items.Refresh();
            }
        }

        public void ThemPhieuGiaCong()
        {
            if (this.maTho.Text == "" || this.hoTenTho.Text == "" ||this.diaChiTho.Text=="" || listGiaCong.Items.Count==0)
            {
                MessageBox.Show("Bạn phải điền đầy đủ thông tin thợ và ds hàng gia công!");
                return;
            }

            PhieuGiaCong_DTO item = new PhieuGiaCong_DTO(this.maPhieu.Text, (DateTime)this.ngayLap.SelectedDate, (DateTime)this.ngayThanhToan.SelectedDate, (DateTime)this.ngayThanhToan.SelectedDate, long.Parse(this.tongTien.Text), this.maTho.Text.ToString());
            if (MessageBox.Show("Bạn Muốn Lập Phiếu Gia Công Này?", "Thông Báo",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                if (PhieuGiaCong_BUS.Instance.ThemPhieuGiaCong(item) && ThemCTPhieuGiaCong())
             {
                MessageBox.Show("Lập Phiếu Thành Công!");
             }
               
            }
            LayNgayHienTai();
            this.hoTenTho.Text = string.Empty;
            this.diaChiTho.Text = string.Empty;
            this.tenGC.Text = string.Empty;
            this.donGiaGC.Text = string.Empty;
            this.soLuong.Text = string.Empty;
            this.maGC.Text = string.Empty;
            this.maTho.Text = string.Empty;
            this.listGiaCong.Items.Clear();
            TinhTongTien();
            LoadMaPhieuGiaCong();
        }

        public bool ThemCTPhieuGiaCong()
        {
            for (int i = 0; i < listGiaCong.Items.Count;i++ )
            {
                HangGiaCong_DTO _temp = (HangGiaCong_DTO)listGiaCong.Items[i];
                ChiTietPhieuGiaCong_DTO _item = new ChiTietPhieuGiaCong_DTO("", this.maPhieu.Text.ToString(), _temp.MaHangGC, _temp.SoLuong, _temp.DonGia);
                if (!CTPhieuGiaCong_BUS.Instance.ThemCTPhieuGiaCong(_item))
                {
                    MessageBox.Show("Thêm Vào Không Thành Công");
                    return false;
                }
            }
            return true;
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            LoadMaTho();
            LayNgayHienTai();
            LoadMaHangGiaCong();
            LoadMaPhieuGiaCong();
        }
    }
}
